package dk.modified.radioonlineapp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class FileManager {
	boolean mExternalStorageAvailable = false;
	boolean mExternalStorageWriteable = false;
	Context context;
	
	public FileManager(Context context){
		this.context = context;
	}
	
	// Checks if a file with the name of the parameter string exists in the applications internal storage
	public boolean fileExistance(String slug){
		
		File f = context.getFileStreamPath(slug + ".png");
		return f.isFile();
	}
	// Loads a file with a name matching the String slug parameter from the applications internal storage
	public Bitmap LoadBitmapInternalStorage(String slug)
	{
		Bitmap b = null;
	    try {
	    	File f = context.getFileStreamPath(slug + ".png");
	        b = BitmapFactory.decodeStream(new FileInputStream(f));
	        return b;
	        
	    } 
	    catch (FileNotFoundException e) 
	    {
	        //e.printStackTrace();
	        return null;
	    }

	}
	// This method has 2 parameters, one is a bitmap image the other is a slug, the slug can be thought of as the
	// filename of the file that will be saved into the applications internal storage
	public void SaveBitmapInternalStorage(Bitmap bitmapImage, String slug){
        FileOutputStream fos = null;
        try {           
            fos = context.openFileOutput(slug + ".png", Context.MODE_PRIVATE);

       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 50, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
